<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width:200px;margin-bottom:5px"
        v-model="params.orgid"
        placeholder="请输入店铺ID"
        clearable
      ></el-input>
      <el-input
        style="width:200px;margin-bottom:5px"
        v-model="params.code"
        placeholder="请输入订单编码"
        clearable
      ></el-input>
      <el-date-picker type="date" v-model="params.date" placeholder="请选择订单日期"></el-date-picker>
      <el-select v-model="params.status" placeholder="请选择订单状态">
        <el-option v-for="(item) in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>

      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
      >查询</el-button>
      <el-button size="small" type="primary" class="handleSearch" @click="getErpEnquiry()">拉取ERP要货单</el-button>
    </div>

    <el-table max-height="600":data="tableData" border style="width: 100%" empty-text="暂无数据">
      <el-table-column
        :prop="item.align"
        :label="item.label"
        :align="item.align"
        :width="item.width"
        v-for="(item, index) in column"
        :key="index"
      >
        <template slot-scope="scope">
          <div v-if="item.prop == 'cz'">
            <el-button
              v-if="scope.row.status == 1"
              type="primary"
              size="mini"
              icon="el-icon-info"
              @click="sendDelivery(scope)"
            >发货</el-button>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-info"
              @click=" particulars(scope)"
            >查看商品</el-button>
            <el-button type="success" size="mini" icon="el-icon-tickets" @click="print(scope)">打印</el-button>
            <el-button type="info" size="mini" icon="el-icon-tickets" @click="derive(scope)">导出</el-button>
          </div>
          <div v-else-if="item.prop == 'status'">
            {{
            scope.row[item.prop] == 0 && '待提交' ||
            scope.row[item.prop] == 1 && '待发货' ||
            scope.row[item.prop] == 2 && '已发货' ||
            scope.row[item.prop] == 3 && '已收货' || ''
            }}
          </div>
          <div v-else>{{scope.row[item.prop]}}</div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination @page="handleSizeChange" @pagesize="handleCurrentChange" :pageData="pageData"></pagination>

    <!-- 详情Dailog -->
    <shopOrderDialog :option="shopOrderDialog" @refresh="refresh"></shopOrderDialog>
  </div>
</template>

<script>
import {
  shopOrderList,
  getErpEnquiry,
  findDelivery
} from "@/api/enquiry/enquiryList";
import { parseTime } from "@/utils/index";
import Pagination from "@/components/pagination";
import ShopOrderDialog from "@/components/enquiry/shopOrderDialog/list";
import { sendDelivery } from "@/api/delivery/deliveryList";
import { getToken} from "@/utils/auth";
export default {
  components: { Pagination, ShopOrderDialog },
  data() {
    return {
      shopOrderDialog: {
        show: false,
        id: ""
      },
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      params: {
        orgid: "",
        code: "",
        date: new Date(),
        status: -1
      },
      options: [
        { id: -1, name: "全部" },
        { id: 0, name: "待提交" },
        { id: 1, name: "待发货" },
        { id: 2, name: "已发货" },
        { id: 3, name: "已收货" }
      ],
      column: [
        {
          label: "订单code",
          prop: "apfromorgcode",
          width: "",
          align: "center"
        },
        { label: "店铺名称", prop: "orgname", width: "", align: "center" },
        { label: "店铺编码", prop: "orgcode", width: "", align: "center" },
        { label: "货单状态", prop: "status", width: "", align: "center" },
        { label: "要货时间", prop: "enquiry_time", width: "", align: "center" },
        { label: "拉取时间", prop: "add_time", width: "", align: "center" },
        { label: "操作", prop: "cz", width: "400px", align: "center" }
      ]
    };
  },

  created() {
    this.getData();
  },
  methods: {
    refresh() {
      this.getData();
    },
    //页面数据
    getData() {
      const data = {
        orgid: this.params.orgid || "-1",
        code: this.params.code,
        date: parseTime(this.params.date).split(" ")[0],
        status: this.params.status,
        limit: this.pageData.pageSize,
        page: this.pageData.page
      };
      shopOrderList(data)
        .then(res => {
          if (res.code == 0) {
            res.data.data.forEach(res => {
              res.add_time = parseTime(res.add_time);
            });
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //打印
    print(scope) {
      console.log(scope);
      this.$router.push({
        path: "/download",
        query: {
          id: scope.row.id
        }
      });
    },
    // 搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      if (this.params.date == null) {
        this.$message({
          type: "info",
          message: "请选择时间"
        });
      } else {
        this.getData();
      }
    },
    //    导出
    derive(scope) {
      let link = document.createElement("a");
      link.href = `${this.$axios.defaults.baseURL}/api/Delivery/deliveryExport?token=${getToken()}&id=${
        scope.row.id
      }&export=1`;
      link.click();
    },
    // erp搜索
    getErpEnquiry() {
      const data = {
        orgid: this.params.orgid || "-1",
        date: parseTime(this.params.date).split(" ")[0]
      };
      getErpEnquiry(data)
        .then(res => {
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: res.msg
            });
            this.handleSearch();
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //详情
    particulars(scope) {
      this.shopOrderDialog.show = true;
      this.shopOrderDialog.id = scope.row.id;
      this.shopOrderDialog.status = scope.row.status;
    },
    sendDelivery(scope) {
      sendDelivery({ id: scope.row.id }).then(res => {
        if (res.code == 0) {
          this.$message({
            type: "success",
            message: res.msg
          });
          this.handleSearch();
        }
      });
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>